package com.feng.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {

	
	/**
	 * 保存cookie
	 * @param response
	 * @param name
	 * @param value
	 */
	public static void saveCookie(HttpServletResponse response,String name,String value) {
		
		//服务端格式
		response.setCharacterEncoding("UTF-8");
		
		//客户端以utf-8方式接受
		response.setContentType("text/html;charset=UTF-8");
		
		response.addCookie(new Cookie(name,value));
	}
	
	/**
	 * 获取用户cookie
	 * @param response
	 * @param request
	 */
	public static String getUserCookie(HttpServletRequest request,String cookieName) {
		//获取cookies数组
		Cookie[] cookies = request.getCookies();
		
		if(CommonUtils.isNotEmpty(cookies)) {
			for (Cookie cookie : cookies) {
				if(cookieName.equals(cookie.getName())) {
					return cookie.getValue();
				}
			}
		}else {
			return null;
		}
		
		return null;
	}
}
